package com.kunbo.cn.service.impl.core.purchase;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.dao.core.purchase.SalesVolumeDao;
import com.kunbo.cn.dao.core.purchase.SalesVolumeHistoryDao;
import com.kunbo.cn.entity.core.purchase.SalesVolumeHistory;
import com.kunbo.cn.entity.core.purchase.SalesVolume;
import com.kunbo.cn.service.core.purchase.SalesVolumeService;

@Service
public class SaleVolumeServiceImpl implements SalesVolumeService {
	
	@Autowired
	private SalesVolumeDao dao;
	
	@Autowired
	private SalesVolumeHistoryDao historyDao;
	
	@Autowired
	private JdbcTemplate jdbc;

	@Override
	public Boolean save(String order_number, String order_name, String customer, String settlement_method,
			float number_of_contracts, String create_user_id, String uuid, Integer taskid,String material ,String material_type,Float unit_price_of_material, Float contract_amount) {
		// TODO Auto-generated method stub
		SalesVolume volume = new SalesVolume();
		volume.setOrder_number(order_number);
		volume.setOrder_name(order_name);
		volume.setCustomer(customer);
		volume.setSettlement_method(settlement_method);
		volume.setNumber_of_contracts(number_of_contracts);
		volume.setCreate_user_id(create_user_id);
		volume.setUuid(uuid);
		volume.setTaskid(taskid);
		volume.setAdjustment_amount(0.0f);
		volume.setMaterial(material);
		volume.setMaterial_classification(material_type);
		volume.setUnit_price_of_material(unit_price_of_material);
		volume.setContract_amount(contract_amount);
		volume = dao.save(volume);
		if(volume !=null)
			return true;
		else
			return false;
	}

	@Override
	public Boolean update(Integer id, String order_number, String order_name, String customer,
			String material_classification, String material, String settlement_method, Float adjustment_amount,
			Float number_of_contracts, String remarks) {
		// TODO Auto-generated method stub
		
		SalesVolume volume = dao.findOne(id);
		volume.setOrder_number(order_number);
		volume.setOrder_name(order_name);
		volume.setCustomer(customer);
		volume.setMaterial_classification(material_classification);
		volume.setMaterial(material);
		volume.setSettlement_method(settlement_method);
		volume.setAdjustment_amount(adjustment_amount);
		volume.setNumber_of_contracts(number_of_contracts);
		volume.setRemarks(remarks);
		volume = dao.save(volume);
		if (volume != null)
			return true;
		else
			return false;
	}

	@Override
	public SalesVolume sel(Integer id) {
		// TODO Auto-generated method stub
		SalesVolume volume = dao.findOne(id);
		if (volume != null)
			return volume;
		else
			return null;
	}

	@Override
	public List<Map<String, Object>> selByCondition(String order_number, String material, String customer, Integer page,
			Integer pageSize) {
		// TODO Auto-generated method stub
		
		String sql = "select * from sales_volume where 1 = 1";
		if(order_number != null && !order_number.equals("")) {
			sql += " and  order_number like  '%" + order_number + "%'";
		}
		if(material != null && !material.equals("")) {
			sql += " and material like '%" + material + "%'";
		}
		if(customer != null && !customer.equals("")) {
			sql += " and customer like '%" + customer + "%'";
		}
		sql += " limit " + page + "," + pageSize;
		System.out.println(sql);
		
		  List<Map<String, Object>> list =   jdbc.queryForList(sql);
		  
		  Map<String,Object> map = new HashMap<String,Object>();
		  map.put("count", list.size());
		  list.add(map);
		  return list;
	}

	

}
